package com.suoluo.zenadmin.service.dest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.dao.dest.DestTrafficDAO;
import com.suoluo.zenadmin.service.ServiceException;
import com.suoluo.zenadmin.vo.dest.DestTraffic2VO;

public class DestTrafficServiceImpl implements DestTrafficService {

	@Autowired
	private DestTrafficDAO destTrafficDAO;
	/**
	 * 目的地顶层交通信息的父id 
	 */
	private static final int PAEENT_ID = 0;
	/**
	 * 目的地交通的第一级
	 */
	private static final int LEVEL1 = 1;
	/**
	 * 目的地交通的第二级
	 */
	private static final int LEVEL2 = 2;
	/**
	 * 目的地交通的第三级
	 */
	private static final int LEVEL3 = 3;

	@Override
	public List<DestTraffic2VO> findDestTrafficLevel1ByDestId(int destId) {
		
		return destTrafficDAO.findDestTrafficLevel1ByDestId(destId);
	}

	@Override
	public void saveDestTrafficLevel1(DestTraffic2VO traffic) {
		//设置交通的父Id
		traffic.setParentId(PAEENT_ID);
		//设置交通的层级
		traffic.setLevel(LEVEL1);
		List<DestTraffic2VO> traff = destTrafficDAO.findDestTrafficByDestIdAndTrafficType(traffic.getDestId(),traffic.getTrafficType());
		if(traff != null && traff.size() != 0){
			throw new ServiceException(CommonResp.SUCCESS_CODE,"该目的地已添加过这种顶层的交通类型，顶层类型只能添加一次！");
		}
		destTrafficDAO.saveDestTrafficLevel(traffic);
	}

	@Override
	public DestTraffic2VO findDestTraffic2ById(int trafficId) {
		
		return destTrafficDAO.findDestTraffic2ById(trafficId);
	}

	@Override
	public void updateDestTraffic2(DestTraffic2VO traffic) {
		//判断类别是否修改了
		int trafficType = traffic.getTrafficType();
		int oldTrafficType = traffic.getOldTrafficType();
		if(trafficType != oldTrafficType){
			List<DestTraffic2VO> traff = destTrafficDAO.findDestTrafficByDestIdAndTrafficType(traffic.getDestId(), traffic.getTrafficType());
			if(traff != null && traff.size() != 0){
				throw new ServiceException(CommonResp.SUCCESS_CODE,"该目的地已添加过这种顶层的交通类型，顶层类型只能添加一次！");
			}
			//修改交类别项下的类别信息
			destTrafficDAO.updateDestTrafficLevelTrafficType(traffic.getDestId(),trafficType,oldTrafficType);
		}
		//修改交通信息
		destTrafficDAO.updateDestTraffic2(traffic);
		
	}

	@Override
	public void deleteDestTraffic(int destId,int trafficType) {
		destTrafficDAO.deleteDestTraffic(destId,trafficType);
	}

	@Override
	public List<DestTraffic2VO> findListDestTrafficLevel(int parentId) {
		return destTrafficDAO.findListDestTrafficLevel(parentId);
	}

	@Override
	public void saveDestTrafficLevel2(DestTraffic2VO traffic) {
		//设置交通信息的级别
		traffic.setLevel(LEVEL2);
		destTrafficDAO.saveDestTrafficLevel(traffic);
	}

	@Override
	public void updateDestTrafficLevel(DestTraffic2VO traffic) {
		destTrafficDAO.updateDestTraffic2(traffic);
		
	}

	@Override
	public void deleteDestTrafficLevel2(int trafficId) {
		destTrafficDAO.deleteDestTrafficLevel2(trafficId);
	}

	@Override
	public void saveDestTrafficLevel3(DestTraffic2VO traffic) {
		traffic.setLevel(LEVEL3);
		destTrafficDAO.saveDestTrafficLevel(traffic);
	}

	@Override
	public void deleteDestTrafficLevel3(int trafficId) {
		destTrafficDAO.deleteDestTrafficLevel3(trafficId);
		
	}

	@Override
	public List<DestTraffic2VO> findAllDestTraffic(int destId) {
		
		//查询目的地下所有的交通信息
		List<DestTraffic2VO> list =  destTrafficDAO.findDestAllTraffices(destId);
		
		Map<Integer,DestTraffic2VO> map1 = new HashMap<Integer, DestTraffic2VO>();
		//获取第一级交通信息
		for (DestTraffic2VO traffic : list) {
			String content = UBB.decode(traffic.getContent());
			traffic.setContent(content);
			if(traffic.getLevel() != 3){
				map1.put(traffic.getTrafficId(), traffic);
			}
		}
		
		List<DestTraffic2VO> trafficList = new ArrayList<DestTraffic2VO>();
		for (DestTraffic2VO traffic : list) {
			if (traffic.getLevel() == 1) {
				trafficList.add(traffic);
			}
			//获取层级交通信息
			DestTraffic2VO tra1 = map1.get(traffic.getParentId());
			if(tra1 != null){
				if(tra1.getSubTraffic() == null){
					tra1.setSubTraffic(new ArrayList<DestTraffic2VO>());
				}
				tra1.getSubTraffic().add(traffic);
			}
		}
		
		return trafficList;
	}
	
	
	
	@Override
	public List<DestTraffic2VO> findTrafficEntity(int trafficId){
		return destTrafficDAO.findTrafficEntity(trafficId);
	}

	@Override
	public List<DestTraffic2VO> findAllDestTrafficNoLevel(int destId) {
		//查询目的地下所有的交通信息
		List<DestTraffic2VO> list =  destTrafficDAO.findDestAllTraffices(destId);
		return list;
	}
}
